package cn.dankal.flyaudio.store;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.CategoryListEntity;
import cn.dankal.basiclib.pojo.ProductEntity;
import cn.dankal.basiclib.util.SharePreferenceManager;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.ClassificationStoreAdapter;
import cn.dankal.flyaudio.adapter.ClassificationTabAdapter;
import cn.dankal.flyaudio.contract.ClassificationContract;
import cn.dankal.flyaudio.presenter.ClassificationPresenter;

public class ClassificationActivity extends BaseActivity implements View.OnClickListener, ClassificationContract.View {

    private MultipleStatusView statusView;
    private RecyclerView tabListView;
    private RecyclerView storeListView;
    private TextView salesView;
    private TextView pricesView;
    private TextView evaluateView;
    private TextView tvAll;
    private RelativeLayout rlSales, rlPrice, rlEvaluate, rlAll;
    private ImageView ivSales;
    private ClassificationTabAdapter tabAdapter;
    private ClassificationStoreAdapter storeAdapter;
    private ClassificationPresenter presenter;
    private String orderBy = "saleCount";
    private String entityId;
//    private List<String> classification = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_classification;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("分类");
        entityId = getIntent().getStringExtra("category_uuid");
        presenter = new ClassificationPresenter();
        presenter.attachView(this);
        statusView = findViewById(R.id.multiple_status_view);
        tabListView = findViewById(R.id.rv_classification_tab);
        storeListView = findViewById(R.id.rv_classification_store);
        salesView = findViewById(R.id.tv_sales);
        pricesView = findViewById(R.id.tv_price);
        evaluateView = findViewById(R.id.tv_evaluate);
        ivSales = findViewById(R.id.iv_sales);
        rlSales = findViewById(R.id.rl_sales);
        rlPrice = findViewById(R.id.rl_price);
        rlEvaluate = findViewById(R.id.rl_evaluate);
        rlAll = findViewById(R.id.rl_all);
        tvAll = findViewById(R.id.tv_all);

        rlAll.setOnClickListener(this);
        rlSales.setOnClickListener(this);
        rlPrice.setOnClickListener(this);
        rlEvaluate.setOnClickListener(this);
        CategoryListEntity categoryListEntity = new CategoryListEntity();
        categoryListEntity.setUuid("");
        categoryListEntity.setName("全部分类");
        tabAdapter = new ClassificationTabAdapter();
        storeAdapter = new ClassificationStoreAdapter();
        tabListView.setLayoutManager(new LinearLayoutManager(this));
        tabListView.setAdapter(tabAdapter);
        storeListView.setLayoutManager(new LinearLayoutManager(this));
        storeListView.setAdapter(storeAdapter);
        tabAdapter.addData(categoryListEntity);
        tabAdapter.addDatas(SharePreferenceManager.getCachedStoreCategory(), false);
        tabAdapter.setSelect(entityId);
        tabAdapter.setListener(new ClassificationTabAdapter.OnTabChangeListener() {
            @Override
            public void onTabChange(CategoryListEntity entity) {
                entityId = entity.getUuid();
                getData();
            }
        });
        rlAll.callOnClick();
    }

    private void getData() {
        boolean asc = false;
        if (orderBy.equals("saleCount")) {
            asc = sales;
        } else if (orderBy.equals("showPrice")) {
            asc = price;
        } else if (orderBy.equals("goodCommentCount")) {
            asc = evaluate;
        } else {
            asc = true;
        }
        if (TextUtils.isEmpty(entityId)) {
            presenter.getShopListByCategoryAndSort("", orderBy, asc, true);
        } else {
            presenter.getShopListByCategoryAndSort(entityId, orderBy, asc, true);
        }
    }

    private boolean sales = true;
    private boolean price = true;
    private boolean evaluate = true;

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (R.id.rl_sales == id) {
            salesView.setSelected(true);
            pricesView.setSelected(false);
            evaluateView.setSelected(false);
            tvAll.setSelected(false);
            sales = !sales;
            if (sales) {
                ivSales.setImageResource(R.mipmap.ic_class_down);
            } else {
                ivSales.setImageResource(R.mipmap.ic_class_up);
            }

            orderBy = "saleCount";
            getData();
        } else if (R.id.rl_price == id) {
            salesView.setSelected(false);
            pricesView.setSelected(true);
            evaluateView.setSelected(false);
            tvAll.setSelected(false);
            orderBy = "showPrice";
            price = !price;

            getData();
        } else if (R.id.rl_evaluate == id) {
            salesView.setSelected(false);
            pricesView.setSelected(false);
            evaluateView.setSelected(true);
            tvAll.setSelected(false);
            orderBy = "goodCommentCount";
            evaluate = !evaluate;

            getData();
        } else if (R.id.rl_all == id) {
            orderBy = "";
            tvAll.setSelected(true);
            salesView.setSelected(false);
            pricesView.setSelected(false);
            evaluateView.setSelected(false);
            getData();
        }
    }

    @Override
    protected void onDestroy() {
        presenter.detachView();
        super.onDestroy();
    }

    @Override
    public void showProductList(List<ProductEntity> data, boolean isRefresh) {
        if (data.isEmpty()) {
            statusView.showEmpty();
            storeAdapter.clearData();
            return;
        }
        statusView.showContent();
        storeAdapter.addDatas(data, isRefresh);
    }
}
